#include "widget.h"
#include "ui_widget.h"

void Widget::readFile()
{
    QFile file("city.txt");
    if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        qDebug()<<"文件打开失败"<<__LINE__<<endl;
        return ;
    }
    City c;
    QTextStream in(&file);
    while(!in.atEnd())
    {
        in>>c;
        city.insert(c.getKey(),c);
    }
    file.close();
}

void Widget::writerFile()
{
    QFile file("city.txt");
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        qDebug()<<"文件写入失败"<<endl;
        return ;
    }
    QTextStream out(&file);

    for(auto e : city)
    {
        if(e.getKey() == "") continue;
        out << e <<endl;
    }

    file.close();
}

void Widget::printTableHead(QStringList head)
{
    ui->tableWidget->clear();
    ui->tableWidget->setColumnCount(head.size());
    ui->tableWidget->setHorizontalHeaderLabels(head);
}

void Widget::printTable(QVector<City> &vc)
{
    ui->tableWidget->setRowCount(vc.size());
    for(int i = 0; i < vc.size(); i++)
    {
        int j = 0;
        if(vc[i].get_country()!="")
        {
            ui->tableWidget->setItem(i,j,new QTableWidgetItem(vc[i].get_country()));
            ui->tableWidget->item(i,j)->setTextAlignment(Qt::AlignCenter);
            j++;
        }
//        qDebug()<<__LINE__<< j << endl;
        if(vc[i].get_name()!="")
        {
            ui->tableWidget->setItem(i,j,new QTableWidgetItem(vc[i].get_name()));
            ui->tableWidget->item(i,j)->setTextAlignment(Qt::AlignCenter);
            j++;
        }
//        qDebug()<<__LINE__<< j << endl;
        if(vc[i].get_day()!=QDate(1,1,1))
        {
            ui->tableWidget->setItem(i,j,new QTableWidgetItem(vc[i].get_day().toString("yyyy年MM月dd日")));
            ui->tableWidget->item(i,j)->setTextAlignment(Qt::AlignCenter);
            j++;
        }
//        qDebug()<<__LINE__<< j << endl;
        if(vc[i].get_new()>=0)
        {
            ui->tableWidget->setItem(i,j,new QTableWidgetItem(QString::number(vc[i].get_new())));
            ui->tableWidget->item(i,j)->setTextAlignment(Qt::AlignCenter);
            j++;
        }
//        qDebug()<<__LINE__<< j << endl;
        if(vc[i].get_now()>=0)
        {
            ui->tableWidget->setItem(i,j,new QTableWidgetItem(QString::number(vc[i].get_now())));
            ui->tableWidget->item(i,j)->setTextAlignment(Qt::AlignCenter);
            j++;
        }
//        qDebug()<<__LINE__<< j << endl;
        if(vc[i].get_all()>=0)
        {
            ui->tableWidget->setItem(i,j,new QTableWidgetItem(QString::number(vc[i].get_all())));
            ui->tableWidget->item(i,j)->setTextAlignment(Qt::AlignCenter);
            j++;
        }
//        qDebug()<<__LINE__<< j << endl;
        if(vc[i].get_cure()>=0)
        {
            ui->tableWidget->setItem(i,j,new QTableWidgetItem(QString::number(vc[i].get_cure())));
            ui->tableWidget->item(i,j)->setTextAlignment(Qt::AlignCenter);
            j++;
        }
//        qDebug()<<__LINE__<< j << endl;
        if(vc[i].get_dead()>=0)
        {
            ui->tableWidget->setItem(i,j,new QTableWidgetItem(QString::number(vc[i].get_dead())));
            ui->tableWidget->item(i,j)->setTextAlignment(Qt::AlignCenter);
            j++;
        }
//        qDebug()<<__LINE__<< j << endl;
    }
    ui->tableWidget->resizeColumnsToContents();
}

void Widget::setCityName()
{
    for(auto e : city)
    {
        city_name.insert(e.get_name());
    }
}

void Widget::setCountryName()
{
    for(auto e : city)
    {
        country_name.insert(e.get_country());
    }
}

QStringList Widget::setTablehead(QString str)
{
    QStringList tablehead = QStringList{"国家"};

    if(Table_flag == true)
    {
        tablehead.append("城市");
        tablehead.append("日期");
    }
    tablehead.append(str);

    return tablehead;
}

QVector<City> Widget::setTableValue(QString str)
{
    QString name = "";

    QVector<City> ret;
    int n = -1;

    for(auto e : city)
    {
        City cnt("",e.get_country(),n,n,n,n,n,QDate(1,1,1));
//        qDebug()<<__LINE__<<endl;

//            qDebug()<<cnt.getKey()<<"  " << cnt.get_name() << "  "
//                    <<cnt.get_country()<<"  " << cnt.get_new() << "  "
//                      <<cnt.get_now()<<"  " << cnt.get_cure() << "  "
//                        <<cnt.get_dead()<<"  " << cnt.get_all() << "  ";
        if(Table_flag)
        {
            cnt.set_name(e.get_name());
            cnt.set_day(e.get_day());
        }
        if(str == "新增病例")
        {
            cnt.set_new(e.get_new());
        }
        else if(str == "现有病例")
        {
            cnt.set_now(e.get_now());
        }
        else if(str == "治愈病例")
        {
            cnt.set_cure(e.get_cure());
        }
        else if(str == "死亡病例")
        {
            cnt.set_dead(e.get_dead());
        }
        else if(str == "累计病例")
        {
            cnt.set_all(e.get_all());
        }
        else if(str=="all")
        {
            cnt.set_new(e.get_new());
            cnt.set_now(e.get_now());
            cnt.set_cure(e.get_cure());
            cnt.set_dead(e.get_dead());
            cnt.set_all(e.get_all());
        }
//        qDebug()<<__LINE__<<endl;

//            qDebug()<<cnt.getKey()<<"  " << cnt.get_name() << "  "
//                    <<cnt.get_country()<<"  " << cnt.get_new() << "  "
//                      <<cnt.get_now()<<"  " << cnt.get_cure() << "  "
//                        <<cnt.get_dead()<<"  " << cnt.get_all() << "  ";
        ret.push_back(cnt);
    }
//    auto& vc = ret;
//    qDebug()<<__LINE__<<endl;

//        qDebug()<<vc[0].getKey()<<"  " << vc[0].get_name() << "  "
//                <<vc[0].get_country()<<"  " << vc[0].get_new() << "  "
//                  <<vc[0].get_now()<<"  " << vc[0].get_cure() << "  "
//                    <<vc[0].get_dead()<<"  " << vc[0].get_all() << "  ";
    if(Table_flag == false)
    {
        QVector<City> cnt;
        for(auto e : country_name)
        {
            City c;
            bool falg = false;
            c.set_country(e);
            for(auto v : ret)
            {
                if(v.get_country() == e)
                {
                    c += v;
                    falg = true;
                }
            }
            if(falg)
            {
                cnt.push_back(c);
            }
        }
        ret = cnt;
    }
    return ret;
}

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
    , city_add_dialog(nullptr)
    , city_search_dialog(nullptr)
    , city_modify_dialog(nullptr)
    ,forecast_dialog(nullptr)

{
    ui->setupUi(this);

    readFile();
//    ui->tableWidget->setHidden(false);
    setCityName();
    setCountryName();
    this->TableHead_city = QStringList{"国家","城市","日期","新增病例","现有病例","累计病例","治愈病例","死亡病例"};
    this->TableHead_country = QStringList{"国家","新增病例","现有病例","总计病例","治愈病例","死亡病例"};
    this->Table_flag = true;
    printTableHead(TableHead_city);
    QVector<City> v(city.begin(),city.end());
    printTable(v);

}

Widget::~Widget()
{
    writerFile();
    delete ui;
}



void Widget::on_pushButton_city_add_clicked()
{
    Table_flag = true;
    if(this->city_add_dialog == nullptr)
    {
        this->city_add_dialog = new city_add_Dialog(city_name, country_name,this);
    }

    QMessageBox mess;
    int ret = this->city_add_dialog->exec();
    if(ret == QDialog::Accepted)
    {
        City cnt = city_add_dialog->getcit();
        if(city.find(cnt.getKey()) != city.end())
        {
            mess.setText("信息重复，插入失败!");
            mess.exec();
            return;
        }
        else
        {
            city.insert(cnt.getKey(),cnt);
            mess.setText("插入信息成功!");
            setCityName();
            setCountryName();
            printTableHead(this->TableHead_city);
            QVector<City> vc(city.begin(), city.end());
            printTable(vc);
            mess.exec();
        }
    }
    delete this->city_add_dialog;
    this->city_add_dialog = nullptr;
}

void Widget::on_pushButton_city_delete_clicked()
{
    if(Table_flag == false) return;
    int index = ui->tableWidget->currentRow();
    if(index == -1) return;
    QString name = ui->tableWidget->item(index,1)->text();
    QString country = ui->tableWidget->item(index,0)->text();
    for(auto it = city.begin(); it != city.end(); it++)
    {
        if(it.value().get_name() == name && it.value().get_country() == country)
        {
            city.erase(it);
        }
    }

    printTableHead(this->TableHead_city);
    QVector<City> vc(city.begin(), city.end());
    printTable(vc);
}

void Widget::on_pushButton_city_search_clicked()
{
    if(city_search_dialog == nullptr)
    {
        city_search_dialog = new city_search_Dialog(city_name, country_name, this);
    }
    int ret = city_search_dialog->exec();

    if(ret != QDialog::Accepted)
    {
        delete city_search_dialog;
        city_search_dialog = nullptr;
        return;
    }

    QStringList cnt = city_search_dialog->getComboBoxValue();
    QVector<City> vc;

    for(auto it = city.begin(); it != city.end(); it++)
    {
        if(it.value().get_country() == cnt[0] && it.value().get_name() == cnt[1])
        {
            vc.push_back(it.value());
        }
    }

    printTableHead(this->TableHead_city);
    printTable(vc);
    delete city_search_dialog;
    city_search_dialog = nullptr;
}

void Widget::on_pushButton_city_modify_clicked()
{
    if(Table_flag == false) return;
    int index = ui->tableWidget->currentRow();
    if(index == -1) return;

    QString name = ui->tableWidget->item(index,1)->text();
    QString country = ui->tableWidget->item(index,0)->text();
    QString day = ui->tableWidget->item(index,2)->text();
    QString key = name + country + day;

    City cit = city[key];

    if(city_modify_dialog == nullptr)
    {
        city_modify_dialog = new city_modify_Dialog(city_name, country_name, cit, this);
    }

    QMessageBox mess;
    int ret = city_modify_dialog->exec();
    if(ret == QDialog::Accepted)
    {
        City cnt = city_modify_dialog->get_city();
        city.erase(city.find(key));
        if(city.find(cnt.getKey()) != city.end())
        {
            mess.setText("修改后信息重复,修改失败！");
            city.insert(cit.getKey(),cit);
            return;
        }
        else
        {
            mess.setText("修改成功！");
            city.insert(cnt.getKey(),cnt);
            setCityName();
            setCountryName();
            printTableHead(TableHead_city);
            QVector<City> vc(city.begin(),city.end());
            printTable(vc);
        }
        mess.exec();
    }
    delete city_modify_dialog;
    city_modify_dialog = nullptr;
}

void Widget::on_pushButton_country_search_clicked()
{
    this->Table_flag = false;
    QVector<City> vc;
    for(auto e : country_name)
    {
        City c;
        c.set_country(e);
        for(auto a : city)
        {
            if(a.get_country() == e)
            {
                c += a;
            }
        }
        vc.push_back(c);
    }

//    qDebug()<<vc[0].getKey()<<"  " << vc[0].get_name() << "  "
//            <<vc[0].get_country()<<"  " << vc[0].get_new() << "  "
//              <<vc[0].get_now()<<"  " << vc[0].get_cure() << "  "
//                <<vc[0].get_dead()<<"  " << vc[0].get_all() << "  ";

    printTableHead(TableHead_country);
    printTable(vc);
}

void Widget::on_pushButton_city_see_clicked()
{
    this->Table_flag = true;
    QVector<City> vc(city.begin(), city.end());
    printTableHead(TableHead_city);
    printTable(vc);
}

void Widget::on_pushButton_country_delete_clicked()
{
    if(Table_flag == true) return;
    int index = ui->tableWidget->currentRow();
    if(index == -1) return;
//    qDebug()<<index<<endl;
    QString country = ui->tableWidget->item(index,0)->text();
//    qDebug()<<__LINE__<<endl;
    for(auto it = city.begin(); it != city.end(); it++)
    {
        if(it.value().get_country() == country)
        {
            city.erase(it);
        }
    }
    qDebug()<<__LINE__<<endl;
    QVector<City> vc = setTableValue("all");
    //qDebug()<<vc.begin()->getKey()<<endl;
//    AAqDebug()<<__LINE__<<endl;
    printTableHead(TableHead_country);
//    qDebug()<<__LINE__<<endl;
    printTable(vc);
}

void Widget::on_pushButton_count_new_clicked()
{
   QVector<City> vc = setTableValue("新增病例");
   QStringList tablehead = setTablehead("新增病例");

   qSort(vc.begin(),vc.end(),[](City& a, City& b)
   {
       return a.get_new() > b.get_new();
   });
   printTableHead(tablehead);
   printTable(vc);
}

void Widget::on_pushButton_count_now_clicked()
{
    QVector<City> vc = setTableValue("现有病例");
//    qDebug()<<__LINE__<<endl;
    QStringList tablehead = setTablehead("现有病例");
// qDebug()<<__LINE__<<endl;
    qSort(vc.begin(),vc.end(),[](City& a, City& b)
    {
        return a.get_now() > b.get_now();
    });

//     qDebug()<<__LINE__<<endl;

//         qDebug()<<vc[0].getKey()<<"  " << vc[0].get_name() << "  "
//                 <<vc[0].get_country()<<"  " << vc[0].get_new() << "  "
//                   <<vc[0].get_now()<<"  " << vc[0].get_cure() << "  "
//                     <<vc[0].get_dead()<<"  " << vc[0].get_all() << "  ";

     printTableHead(tablehead);
    printTable(vc);
}

void Widget::on_pushButton_count_all_clicked()
{
    QVector<City> vc = setTableValue("累计病例");
    QStringList tablehead = setTablehead("累计病例");

    qSort(vc.begin(),vc.end(),[](City& a, City& b)
    {
        return a.get_all() > b.get_all();
    });
    printTableHead(tablehead);
    printTable(vc);
}

void Widget::on_pushButton_count_cure_clicked()
{
    QVector<City> vc = setTableValue("治愈病例");
    QStringList tablehead = setTablehead("治愈病例");

    qSort(vc.begin(),vc.end(),[](City& a, City& b)
    {
        return a.get_cure() > b.get_cure();
    });
    printTableHead(tablehead);
    printTable(vc);
}

void Widget::on_pushButton_count_dead_clicked()
{
    QVector<City> vc = setTableValue("死亡病例");
    QStringList tablehead = setTablehead("死亡病例");

    qSort(vc.begin(),vc.end(),[](City& a, City& b)
    {
        return a.get_dead() > b.get_dead();
    });
    printTableHead(tablehead);
    printTable(vc);
}


void Widget::on_pushButton_forecast_clicked()
{
    if(forecast_dialog == nullptr)
    {
        forecast_dialog = new forecast_Dialog(city,city_name,country_name,this);
    }
    forecast_dialog->exec();

    delete forecast_dialog;
    forecast_dialog = nullptr;
}
